//
//  ProjectListViewController.m
//  OutesIOS
//
//  Created by norman on 2019/1/5.
//  Copyright © 2019 norman. All rights reserved.
//

#import "ProjectListViewController.h"
#import "ProjectDetailViewController.h"
#import "ProjectStatusModifyViewController.h"
#import "ProjectManager.h"
#import "ProjectData.h"
#import "ProjectManager.h"
#import "Project_Step8ViewController.h"
#import "TwoLineWithDateCell.h"
#import "ProjectTableViewCell.h"
#import "ProjectLIstResp.h"
#import "Project_Step1ViewController.h"

#define CELL_IDENTIFIER @"CELL_IDENTIFIER"

@interface ProjectListViewController ()

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;

@end

@implementation ProjectListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupViews];
    [self initData];
}

#pragma mark - 初始化视图
- (void)setupViews{
    self.tableView = ({
        UITableView *tableView = [[UITableView alloc] initWithFrame:[UIView frameWithOutNavSegment] style:UITableViewStylePlain];
        [tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [tableView registerClass:[ProjectTableViewCell class] forCellReuseIdentifier:CELL_IDENTIFIER];
        [self.view addSubview:tableView];
        tableView;
    });
    
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
    [self.tableView.mj_header beginRefreshing];
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

#pragma mark - 初始化数据
- (void)initData{
    self.dataSource = [NSMutableArray array];
}

- (void)refreshData {
    // 马上进入刷新状态
    [self loadAllProjects:self.type];
}


- (void)loadAllProjects:(NSInteger) phase {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:[NSNumber numberWithInteger:phase] forKey:@"state"];
    
    [HttpClient getWithUrl:HTTP_GET_PROJECT_LIST_ALL token:[[AccountManager manager]token] paramDic:param resultClass:[ProjectListResp class] success:^(ProjectListResp *obj) {
        //下拉刷新
        [self.tableView.mj_header endRefreshing];
        [self.dataSource removeAllObjects];
        if (obj.result && obj.result.count > 0) {
            [self.dataSource addObjectsFromArray:obj.result];
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.dataSource removeAllObjects];
        [self.tableView reloadData];
        CMLog(@"获取项目失败：%@", error);
    }];
}

#pragma mark - UITableView代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    ProjectListResp *group = [self.dataSource objectAtIndex:section];
    return group.projects.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ProjectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELL_IDENTIFIER];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    ProjectListResp *group = [self.dataSource objectAtIndex:indexPath.section];
    ProjectData *item = [group.projects objectAtIndex:indexPath.row];
    NSString *area = [NSString stringWithFormat:@"%d ㎡", (int)item.area];
//    [cell setProjectName:item.project_name andStatus:item.is_lock andContact:item.owner_name andTelephone:item.owner_tel andCreator:item.name andArea:area andCity:item.city_name];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [ProjectTableViewCell cellHeight];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ProjectListResp *group = [self.dataSource objectAtIndex:indexPath.section];
    ProjectData *item = [group.projects objectAtIndex:indexPath.row];
    if (self.projectSelection) {
        self.projectSelection(item);
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    ProjectListResp *group = [self.dataSource objectAtIndex:section];
    CGFloat h = [Tools sizeOfSingleLineText:@"高度" withSize:kListTitleFontSize].height + 30;
    
    UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreen_Width, h)];
    header.backgroundColor = [UIColor whiteColor];
    //时间
    UILabel *lbDate = [UILabel labelSingleLineFontSize:kListTitleFontSize content:group.date];
    lbDate.textColor = BSBlackTextColor;
    lbDate.width += 10;
    lbDate.y = 30;
    [header addSubview:lbDate];
    
    UIImageView *dot = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"dot"]];
    dot.width = 5;
    dot.height = 5;
    dot.x = 10;
    dot.centerY = lbDate.centerY;
    lbDate.x = CGRectGetMaxX(dot.frame) + 5;
    [header addSubview:dot];
    return header;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    CGFloat h = [Tools sizeOfSingleLineText:@"高度" withSize:kListTitleFontSize].height + 30;
    return h;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreen_Width, CGFLOAT_MIN)];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
